﻿using System.Collections.Generic;
using System.Data;
using System.Text;
using LL.Common;
using LL.Data;
using FluentData;

namespace LL.Data.FluentData
{
    /// <summary>
    /// 基础查询仓储
    /// </summary>
    public class DbSessionBase : ITransaction
    {
        #region 属性

        protected bool UseTransaction { get; set; }
        IDbContext _DbContext = null;

        /// <summary>
        /// 当前数据访问对象上下文
        /// </summary>
        public IDbContext DbContext
        {
            get
            {
                return _DbContext;
            }
            set
            {
                _DbContext = value;
#if Debug
                Debug();
#endif
            }
        }

        #endregion

        #region ITransaction

        /// <summary>
        /// 提交
        /// </summary>
        public void Commit()
        {
#if Debug
            System.Diagnostics.Debug.WriteLine(DbContext.GetHashCode() + "[√][Commit           ]");
#endif
            DbContext = DbContext.Commit();
        }

        /// <summary>
        /// 回滚
        /// </summary>
        public void Rollback()
        {
#if Debug
            System.Diagnostics.Debug.WriteLine(DbContext.GetHashCode() + "[×][Rollback          ]");
#endif
            DbContext = DbContext.Rollback();
        }

        /// <summary>
        /// 超出Using语句块仍未提交
        /// 则自动回滚并释放资源
        /// </summary>
        public void Dispose()
        {
            if (DbContext == null)
                return;
            if (UseTransaction)
                DbContext = DbContext.Rollback();
            DbContext.Dispose();
        }

        public void BeginTranscation()
        {
            UseTransaction = true;
            DbContext.UseTransaction(true);
        }

        #endregion

#if Debug
        private void Debug()
        {

            _DbContext.OnConnectionOpening(d =>
            {
                System.Diagnostics.Debug.WriteLine("-----------------------------------------------------");
                System.Diagnostics.Debug.WriteLine(_DbContext.GetHashCode() + "[■][ConnectionOpening]");
            });
            _DbContext.OnConnectionOpened(d =>
            {
                System.Diagnostics.Debug.WriteLine(_DbContext.GetHashCode() + "[√][ConnectionOpened ]");
            });
            _DbContext.OnConnectionClosed(d =>
            {
                System.Diagnostics.Debug.WriteLine(_DbContext.GetHashCode() + "[■][ConnectionClosed ]");
                System.Diagnostics.Debug.WriteLine("-----------------------------------------------------");
            });
            _DbContext.OnExecuting(d =>
            {
                System.Diagnostics.Debug.WriteLine(_DbContext.GetHashCode() + "[◇][CommandText      ]" + d.Command.CommandText);
                System.Diagnostics.Debug.WriteLine(_DbContext.GetHashCode() + "[◇][ParametersCount  ]" + d.Command.Parameters.Count);
            });
            _DbContext.OnError(d =>
            {
                System.Diagnostics.Debug.WriteLine(DbContext.GetHashCode() + "[×][Error             ]" + d.Exception.Message);
            });
            _DbContext.OnExecuted(d =>
            {
            });
        }
#endif

    }
}
